package com.example.mywechat;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mywechat.adapter.MsgAdapter;
import com.example.mywechat.model.MsgModel;
import com.example.mywechat.util.MsgDataBaseHelper;

import java.util.ArrayList;

public class MsgActivity extends AppCompatActivity {

    private ArrayList<MsgModel> msgList;
    private MsgAdapter mAdapter;
    EditText chatEditView,inputType;
    Spinner spinner;
    ListView chatListView;
    MsgDataBaseHelper msgDataBaseHelper;
    TextView chatFriName;
    int tem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        Intent intent = getIntent();
        String strName = intent.getStringExtra("chatFriendName");
        tem = intent.getIntExtra("tem",1);
        msgDataBaseHelper = new MsgDataBaseHelper(this, "MessageInfo1", null, 3);
        chatListView = findViewById(R.id.chatListView);
        chatEditView = findViewById(R.id.chatEditText);
        chatFriName = findViewById(R.id.chatFriName);
        //spinner = findViewById(R.id.inputType);
        inputType =findViewById(R.id.inputType);
        msgList = new ArrayList<>();
        initMessage();
        mAdapter = new MsgAdapter(msgList, this);
        chatListView.setAdapter(mAdapter);
        chatListView.setSelection(msgList.size());
        chatFriName.setText(strName);
    }

    public void send(View v) {
        String content = chatEditView.getText().toString();
        int type = Integer.valueOf(inputType.getText().toString());
        /*int type;
        String tem = (String) spinner.getSelectedItem();
        if(tem.equals("自己"))
            type = 1;
        else
            type = 0;*/
        if (!"".equals(content)) {
            MsgModel message = new MsgModel(type, content, BitmapFactory.decodeResource(getResources(), type==1?R.drawable.head:R.drawable.tencent));
            msgList.add(message);
            mAdapter.notifyDataSetChanged();
            chatListView.setSelection(msgList.size());
            chatEditView.setText("");
            //另存一条记录到数据库
            SQLiteDatabase db = msgDataBaseHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("type", type);
            values.put("content", content);
            values.put("icon", type==1?R.drawable.head:R.drawable.tencent);
            db.insert("MessageInfo1", null, values);
        }

    }

    @Override
    public void onBackPressed() {
        SQLiteDatabase db = msgDataBaseHelper.getWritableDatabase();
        Cursor cursor = db.query("MessageInfo1", null, null, null, null, null, null);
        cursor.moveToLast();
        String content = cursor.getString(cursor.getColumnIndex("content"));
        startActivity(new Intent(getApplicationContext(),MainActivity.class).putExtra("lastMsg",content).putExtra("tem",tem));
        finish();
        super.onBackPressed();
    }

    private void initMessage() {
        /*MsgModel message1 = new MsgModel(0,"宋慧乔是我女神", BitmapFactory.decodeResource(getResources(),R.drawable.head));
        msgList.add(message1);
        MsgModel message2 = new MsgModel(1,"宋钟基是我男神",BitmapFactory.decodeResource(getResources(),R.drawable.head));
        msgList.add(message2);
        MsgModel message3 = new MsgModel(0,"呵呵",BitmapFactory.decodeResource(getResources(),R.drawable.head));
        msgList.add(message3);
        MsgModel message4 = new MsgModel(1,"呵呵",BitmapFactory.decodeResource(getResources(),R.drawable.head));
        msgList.add(message4);
        MsgModel message5 = new MsgModel(0,"我错了", BitmapFactory.decodeResource(getResources(),R.drawable.head));
        msgList.add(message5);*/
        //数据库读聊天数据
        SQLiteDatabase db = msgDataBaseHelper.getWritableDatabase();
        Cursor cursor = db.query("MessageInfo1", null, null, null, null, null, null);
        cursor.moveToFirst();
        for (int i = 1; i <= cursor.getCount(); i++) {//数据库遍历
            int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex("type")));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            int icon = Integer.parseInt(cursor.getString(cursor.getColumnIndex("icon")));
            MsgModel msg = new MsgModel(type, content, BitmapFactory.decodeResource(getResources(), icon));
            msgList.add(msg);
            cursor.moveToNext();
        }
    }
}
